import 'dart:async';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:star_author/utils/utils.dart';
import 'package:star_author/views/splash_page/splash_ad_util.dart';

import '../../widgets/dialog/dialog_type.dart';
import '../../widgets/dialog/sure_dialog.dart';

/// 开屏广告页
class SplashAdvertisementWidget extends StatefulWidget {
  SplashAdvertisementWidget(this.adImageInfo, this.timeFinishCallback);

  final Function timeFinishCallback;

  final Map adImageInfo;

  @override
  _SplashAdvertisementWidgetState createState() =>
      _SplashAdvertisementWidgetState();
}

class _SplashAdvertisementWidgetState extends State<SplashAdvertisementWidget> {
  Map? _info;

  int _time = 5;

  late Timer _timer;

  File? imageFile;

  @override
  void initState() {
    _info = widget.adImageInfo;

    SplashAdUtil.getAdImagePath(_info ?? {}).then((imagePath) {
      if (mounted) {
        setState(() {
          imageFile = File(imagePath);
        });
      }
    });

    createTimer();

    super.initState();
  }

  void createTimer() {
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (mounted) {
        setState(() {
          _time--;
        });

        if (_time <= 0) {
          widget.timeFinishCallback(false);
          releaseTimer();
        }
      }
      if (!_timer.isActive) {
        timer.cancel();
      }
    });
  }

  void releaseTimer() {
    _timer.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        if (imageFile != null)
          GestureDetector(
            onTap: () async {
              releaseTimer();
              DialogUtil.showMyDialog(
                dialogType: DialogType.open_page_in_splash,
                leftButtonOnTap: () {
                  createTimer();
                },
                rightButtonOnTap: () {
                  _time = 1;
                  releaseTimer();
                  //页面跳转
                  Future.delayed(Duration(milliseconds: 100), () {
                    widget.timeFinishCallback(true);
                  });
                },
              );
            },
            child: Container(
              width: double.infinity,
              height: double.infinity,
              decoration: _info?['colors'].length >= 6
                  ? BoxDecoration(
                      // "colors": ["#FFFDF9", "#FFD89C", -1.0, -1.0, 1.0, 1.0],
                      // index=0,起始颜色
                      // index=1,结束颜色
                      // index=2,3 渐变色起始点
                      // index=4,5 渐变色结束点
                      //index =6 切图的高度比例
                      gradient: LinearGradient(
                        colors: [
                          hexToColor(_info?['colors'][0] ?? '#ffffff'),
                          hexToColor(_info?['colors'][1] ?? '#ffffff'),
                        ],
                        begin: Alignment(_info?['colors'][2] ?? -1.0,
                            _info?['colors'][3] ?? -1.0),
                        end: Alignment(_info?['colors'][4] ?? -1.0,
                            _info?['colors'][5] ?? -1.0),
                      ),
                    )
                  : null,
              alignment: Alignment.center,
              child: Image.file(
                imageFile!,
                height: _info?['colors'].length == 7
                    ? (ScreenUtil().screenHeight * _info?['colors'][6])
                    : ScreenUtil().screenHeight,
                fit: BoxFit.fitHeight,
              ),
            ),
          ),
        Positioned(
          right: 10.w,
          top: ScreenUtil().statusBarHeight,
          child: CupertinoButton(
            onPressed: () {
              widget.timeFinishCallback(false);
              releaseTimer();
            },
            child: Container(
              padding: EdgeInsets.symmetric(
                horizontal: 20.w,
                vertical: 5.w,
              ),
              decoration: BoxDecoration(
                color: Color(0x59424242),
                borderRadius: BorderRadius.all(
                  Radius.circular(
                    30.w,
                  ),
                ),
              ),
              child: Text(
                "跳过 $_time",
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 16.w,
                  decoration: TextDecoration.none,
                ),
              ),
            ),
          ),
        )
      ],
    );
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }
}
